<?php
class Category extends AdminController {
    public function __construct() {
        parent::__construct();
    }
    
    public function index() {
        $this->_data["loadPage"]="category/index_view";
        $this->_data["titlePage"]="Quản lý thể loại";
        $this->_data["mess_succ"]=$this->session->flashdata("flash_mess");
        
        $this->session->unset_userdata("sess_search_title");
        $this->session->unset_userdata("sess_search_parent");
        $this->session->unset_userdata("sess_search_module");
        
        $this->load->model("mcategory");
        $this->load->library("pagination");
        $config=array(
            "base_url"=>base_url()."admin_combo/category/index/",
            "total_rows"=>$this->mcategory->countAll(),
            "per_page"=>$this->_data["perpage"],
            "uri_segment"=>4
        );
        $start=$this->uri->segment(4)==""?0:$this->uri->segment(4);
        $this->pagination->initialize($config);
        if($this->mcategory->listAllLimit($config["per_page"], $start) == false)
            $this->_data["info"] = "empty";
        else
            $this->_data["info"]=$this->mcategory->listAllLimit($config["per_page"], $start);
            //$this->_data["info"]=$this->muser->listAll();
        
        $this->load->helper("menu_cate_helper");
        $this->_data["menu"]=$this->mcategory->listAll();
        
        $this->load->model("mmodule");
        $this->load->helper("module_helper");
        $this->_data["info_module"]=$this->mmodule->listAll();
        
        $this->load->view($this->_data["path"], $this->_data);
    }
    
    public function add() {
        $title=$this->input->post("title");
        $parent=$this->input->post("parent");
        $order=$this->input->post("order");
        $note=$this->input->post("note");
        $module=$this->input->post("module");
        $this->load->model("mcategory");
        if($this->mcategory->checkTitle($title) == false)
            echo "title";
        else {
            $data=array(
                "cate_title"=>$title,
                "cate_parent"=>$parent,
                "cate_order"=>$order,
                "cate_note"=>$note,
                "module_id"=>$module
            );
            $this->mcategory->insert($data);
            $this->session->set_flashdata("flash_mess", "Thêm thể loại thành công.");
            echo "Thêm thể loại thành công!";
        }
    }
    
    public function edit() {
        $this->_data["loadPage"]="category/edit_view";
        $this->_data["titlePage"]="Chỉnh sửa thể loại";
        
        //load data
        $this->load->model("mcategory");
        $id=$this->uri->segment(4);
        $this->load->helper("menu_cate_helper");
        $this->_data["menu"]=$this->mcategory->listAll();
        
        $this->load->model("mmodule");
        $this->load->helper("module_helper");
        $this->_data["info_module"]=$this->mmodule->listAll();
        
        if($this->mcategory->getById($id) != false)
            $this->_data["info_edit"]=$this->mcategory->getById($id);
        
        //validate
        $this->load->helper("form");
        $this->load->library("form_validation");
        $this->form_validation->CI =& $this;
        
        $config=array(
            array(
                "field"=>"txtTitle",
                "label"=>"Tên",
                "rules"=>"callback_checkTitle"
            )
        );
        $this->form_validation->set_rules($config);
        
        //update
        if($this->form_validation->run()) {
            $data=array(
                "cate_title"=>$this->input->post("txtTitle"),
                "cate_parent"=>$this->input->post("slParent"),
                "cate_order"=>$this->input->post("txtOrder"),
                "cate_note"=>$this->input->post("txtNote"),
                "module_id"=>$this->input->post("slModule")
            );
            $this->mcategory->update($data, $id);
            $this->session->set_flashdata("flash_mess", "Chỉnh sửa thể loại thành công.");
            redirect(base_url()."admin_combo/category/");
        }
        
        $this->load->view($this->_data["path"], $this->_data);
    }
    
    public function del() {
        $id=$this->uri->segment(4);
        $this->load->model("mcategory");
        $this->mcategory->del($id);
        $this->session->set_flashdata("flash_mess", "Xóa thể loại thành công.");
        redirect(base_url()."admin_combo/category/");
    }
    
    public function search() {
        $this->_data["loadPage"]="category/search_view";
        $this->_data["titlePage"]="Tìm kiếm thể loại";
        
        $title=$this->session->userdata("sess_search_title");
        $parent=$this->session->userdata("sess_search_parent");
        $module=$this->session->userdata("sess_search_module");
        
        $this->load->model("mcategory");
        $this->load->library("pagination");
        $config=array(
            "base_url"=>base_url()."admin_combo/category/search/",
            "total_rows"=>$this->mcategory->countAllResults($title, $parent, $module)["count"],
            "per_page"=>$this->_data["perpage"],
            "uri_segment"=>4
        );
        $start=$this->uri->segment(4)==""?0:$this->uri->segment(4);
        $this->pagination->initialize($config);
        if($this->mcategory->search($title, $parent, $module, $config["per_page"], $start) == false)
            $this->_data["info"] = "empty";
        else
            $this->_data["info"]=$this->mcategory->search($title, $parent, $module, $config["per_page"], $start);
        
        $this->load->helper("menu_cate_helper");
        $this->_data["menu"]=$this->mcategory->listAll();
        
        $this->load->model("mmodule");
        $this->load->helper("module_helper");
        $this->_data["info_module"]=$this->mmodule->listAll();
        
        $this->load->view($this->_data["path"], $this->_data);
    }
    
    public function searchdata() {
        if(isset($_POST["btnSearchCategory"])) {
            $sess=array(
                "sess_search_title"=>$this->input->post("txtTitle"),
                "sess_search_parent"=>$this->input->post("slParent"),
                "sess_search_module"=>$this->input->post("slModule")
            );
            $this->session->set_userdata($sess);
            redirect(base_url()."admin_combo/category/search");
        }
    }
    
    public function reloadData() {
        $this->load->model("mcategory");

        $this->load->library("pagination");
        $config=array(
            "base_url"=>base_url()."admin_combo/category/index/",
            "total_rows"=>$this->mcategory->countAll(),
            "per_page"=>$this->_data["perpage"],
            "uri_segment"=>4
        );
        $start=$this->uri->segment(4)==""?0:$this->uri->segment(4);
        $this->pagination->initialize($config);
        if($this->mcategory->listAllLimit($config["per_page"], $start) == false)
            $this->_data["info"] = "empty";
        else
            $this->_data["info"]=$this->mcategory->listAllLimit($config["per_page"], $start);
            //$this->_data["info"]=$this->muser->listAll();
        
        $this->load->view("category/reload_view", $this->_data);
        
    }
    
    public function reloadDataFormAction() {
        $this->load->model("mcategory");
        $this->load->helper("menu_cate_helper");
        $this->_data["menu"]=$this->mcategory->listAll();
        
        $this->load->model("mmodule");
        $this->load->helper("module_helper");
        $this->_data["info_module"]=$this->mmodule->listAll();
        
        $this->load->view("category/reload_formaction_view", $this->_data);
    }
    
    public function checkTitle($title) {
        $this->load->model("mcategory");
        $id=$this->uri->segment(4);
        if(!$this->mcategory->checkTitle($title, $id)) {
            $this->form_validation->set_message("checkTitle", "Tiêu đề này đã được đăng ký!");
            return false;
        }else
            return true;
    }
    
    public function testTree() {
        $this->_data["loadPage"]="category/test";
        $this->_data["titlePage"]="Test tree";
        
        $this->load->helper("cate_tree_helper");
        $this->load->model("mcategory");
        $tmp=$this->mcategory->listAll();
        if($tmp!=false) $this->_data["info"]=$tmp;
        
        $this->load->view($this->_data["path"], $this->_data);
    }
}
?>